var navi_line = new Array();
var navi_tb_line = 0;
var navi_linetest = 0;
var navi_linetest_cd = 0;
var navi_linetest_tick = null;


function navi_ajax(i){
  var obj = $(".fn_pic:eq("+i+")");
  var t = new Date().getTime();
  navi_line[i]['t'] = t;
  var url = navi_line[i]['url']+"res/style1/arrow.gif?"+t+"_"+i+Math.random();
  obj.hide();
  obj.html("<IMG class='fn_img' src='"+url+"' navi_id='"+i+"' navi_t='"+t+"' />");
}

function navi_netline_test_tick(){
  if(!navi_linetest_tick){
    navi_linetest_tick = setInterval("navi_netline_test_tick()", 100);
    navi_linetest_cd = 40;
  }
  else if(navi_linetest_tick && --navi_linetest_cd <= 0){
    clearInterval(navi_linetest_tick);
    navi_linetest_tick = null;
    navi_netline_ok();
  }

  var img, navi_id, s;
  $(".fn_img").each(function(){
    img = $(this).get(0);
    if(img.complete){
      navi_id = $(this).attr("navi_id");
      if(img.width > 0){
        var s = parseInt((new Date().getTime() - $(this).attr("navi_t"))/1000);
        navi_line[navi_id]['speed'] = s;
        $(".speed:eq("+navi_id+")").html('OK');
      }
      $(this).removeClass("fn_img");
    }
  });

  if($(".fn_img").length < 1) navi_linetest_cd = 0;
}

function navi_netline_test(){
  if(navi_linetest > 0) return;
  $(".fn_pic").html("");
  navi_close_btn();
  navi_linetest = navi_line.length;
  for(var i=0; i<navi_line.length; ++i){
    navi_line[i]['speed'] = 9999;
  }
  navi_netline_ds();
  for(var i=0; i<navi_line.length; ++i){
    navi_ajax(i);
  }
  navi_netline_test_tick();
}

function navi_netline_del(){
  while(navi_tb_line > 0){
    $("#navi_tb tr:eq(1)").remove();
    --navi_tb_line;
  }
}

function navi_netline_ok(){
  if(navi_linetest < 0) return;
  navi_linetest = -1;
  navi_line.sort(function(a,b){
    if(a['speed'] < b['speed']) return -1;
    if(a['speed'] > b['speed']) return 1;
    return 0;
  });
  navi_netline_ds();
  setTimeout("navi_open_btn()", 3000);
}

function navi_close_btn(){
  $("#btn_test").prop("disabled", true);
}

function navi_open_btn(){
  $("#btn_test").prop("disabled", false);
}


function navi_url(i){
  // if(navi_line[i]["speed"] < 2000 || confirm("此綫路網路繁忙，可能無法訪問，確定要進入？")){
  //   var url = navi_line[i]["url"] + navi_type + "/?/index/"+visit_navi+"/"+i;
  var url = navi_line[i]["url"];
    window.open(url);    
  // }
}